﻿using ASmile.DBModel;
using ASmile.ORM;
using ASmile.ORM.Cores;
using ASmile.ORM.Entitys;

namespace ASmile.Com.Da
{
    class DBCacheEvent
    {
        public static void Init()
        {
            DBMain.OnAfterExec -= ExecAfter;
            DBMain.OnAfterExec += ExecAfter;
            LogHelper.LogObj.Info("系统数据缓存事件初始化完成");
        }

        static void ExecAfter(EntityBase model, EmDbOperation operate, ExecResult result)
        {
            if (result.RowCount == 0) return;

            if (model is Cfg_Dict cfgDict)
            {
                CfgDictExec(cfgDict, operate);
            }
            else if (model is Dict_WordCode wordCode)
            {
                WordCodeExec(wordCode, operate);
            }
            else if (model is Sys_GridColumn gridColumn)
            {
                GridColumnExec(gridColumn, operate);
            }
            else if (model is Sys_RoleUrl roleUrl)
            {
                RoleUrlExec(roleUrl, operate);
            }
        }

        static void CfgDictExec(Cfg_Dict cfgDict, EmDbOperation operate)
        {
            if (operate == EmDbOperation.Create)
            {
                LogHelper.LogObj.Info("不应该有的操作，cfgDictExec Create");
            }
            else if (operate == EmDbOperation.Update)
            {
                DataCacheCom.CfgDictEdit(cfgDict);
            }
            else if (operate == EmDbOperation.Delete)
            {
                LogHelper.LogObj.Info("不应该有的操作，cfgDictExec Delete");
            }
        }

        static void WordCodeExec(Dict_WordCode wordCode, EmDbOperation operate)
        {
            if (operate == EmDbOperation.Create)
            {
                DataCacheCom.WordCodeAdd(wordCode);
            }
            else if (operate == EmDbOperation.Update)
            {
                DataCacheCom.WordCodeEdit(wordCode);
            }
            else if (operate == EmDbOperation.Delete)
            {
                DataCacheCom.WordCodeDel(wordCode.WordName);
            }
        }

        static void GridColumnExec(Sys_GridColumn model, EmDbOperation operate)
        {
            if (operate == EmDbOperation.Create)
            {
                DataCacheCom.GridColumnAdd(model);
            }
            else if (operate == EmDbOperation.Update)
            {
                DataCacheCom.GridColumnEdit(model);
            }
            else if (operate == EmDbOperation.Delete)
            {
                DataCacheCom.GridColumnDel(model.Id);
            }
        }

        static void RoleUrlExec(Sys_RoleUrl model, EmDbOperation operate)
        {
            if (operate == EmDbOperation.Create)
            {
                DataCacheCom.RoleUrlAdd(model);
            }
            else if (operate == EmDbOperation.Update)
            {
                DataCacheCom.RoleUrlEdit(model);
            }
            else if (operate == EmDbOperation.Delete)
            {
                DataCacheCom.RoleUrlDel(model.Id);
            }
        }

        static void ReportExec(Sys_Report model, EmDbOperation operate)
        {
            if (operate == EmDbOperation.Create)
            {
                DataCacheCom.ReportAdd(model);
            }
            else if (operate == EmDbOperation.Update)
            {
                DataCacheCom.ReportEdit(model);
            }
            else if (operate == EmDbOperation.Delete)
            {
                DataCacheCom.ReportDel(model.Id);
            }
        }

        static void ReportArgExec(Sys_ReportArg model, EmDbOperation operate)
        {
            if (operate == EmDbOperation.Create)
            {
                DataCacheCom.ReportArgAdd(model);
            }
            else if (operate == EmDbOperation.Update)
            {
                DataCacheCom.ReportArgEdit(model);
            }
            else if (operate == EmDbOperation.Delete)
            {
                DataCacheCom.ReportArgDel(model.Id);
            }
        }
    }
}